package com.zh.note.huawei.onehundred.数组合并;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: zhanghuan
 * @date: 2023/9/1 22:36
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        sc.nextLine();
        String[] strings = new String[n];
        for (int i = 0; i < strings.length; i++) {
            strings[i] = sc.nextLine();
        }

        StringBuilder sb = new StringBuilder();
        List<Deque<Integer>> lists = new ArrayList<>();
        for (String str : strings) {
            Deque<Integer> list = Arrays.stream(str.split(",")).map(Integer::parseInt).collect(Collectors.toCollection(LinkedList::new));
            lists.add(list);
        }
        int index = 0;
        while (lists.size() > 0) {
            Deque<Integer> temp = lists.get(index);
            for (int i = 0; i < k; i++) {
                if (temp.isEmpty()) {
                    lists.remove(temp);
                    index--;
                    break;
                }
                int data = temp.removeFirst();
                sb.append(data).append(",");
            }
            index++;
            if (index >= lists.size()) {
                index = 0;
            }
        }
        System.out.println(sb.substring(0, sb.length() - 1));

    }
}
